package com.shanhy.lixp.need001.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 请求HTML页面数据
 *
 * @author   单红宇(365384722)
 * @myblog  http://blog.csdn.net/catoop/
 */
@Component
public class QichachaHtmlRequest {

	private static final Logger LOG = LoggerFactory.getLogger(QichachaHtmlRequest.class);
	
	private String cookieString = "acw_tc=AQAAAFAgF2727wUA8d9MLesGtohjyPcp; gr_user_id=73a8ec52-6c83-4f42-87d4-a748616c72cf; UM_distinctid=15bbeb04a70a3b-054ff6f3f50da6-3c365402-c0000-15bbeb04a71a54; _uab_collina=149355287774655980470954; _umdata=0712F33290AB8A6D54189E41F9F357FCCB0A86850F43DD5F26220130CAD166EFC6B9CE4AB60D7DCBCD43AD3E795C914C5A3A96B9DA69B86646F10CBA4D420A77; PHPSESSID=0m7ruab0b52irshkje02vgkua1; gr_session_id_9c1eb7420511f8b2=0d1ed18f-321c-48bd-bec8-18cb90d57c0c; CNZZDATA1254842228=382971284-1493552268-%7C1493807972";

	@Autowired
	private HttpClient httpClient;
	
//	public static void main(String[] args) {
//		System.out.println(new QichachaHtmlRequest().getPageHtml("http://www.qichacha.com/search?key=" + URLEncoder.encodeParam("江苏江南春机电设备工程有限公司", "UTF-8")));
//	}
	
	/**
	 * 根据URL请求页面HTML
	 *
	 * @param pageNum	页数，从1开始 
	 * @return
	 * @author SHANHY
	 */
	public String getPageHtml(String url) {
		GetMethod method = null;
		try {
//			httpClient = new HttpClient();
			
			method = new GetMethod(url);
			
			method.addRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
//			method.addRequestHeader("Accept-Encoding", "gzip, deflate, sdch");
			method.addRequestHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4");
			method.addRequestHeader("Cache-Control", "max-age=0");
			method.addRequestHeader("Connection", "keep-alive");
			method.addRequestHeader("Cookie", cookieString);
			method.addRequestHeader("Host", "www.qichacha.com");
			method.addRequestHeader("Upgrade-Insecure-Requests", "1");
			method.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
			method.addRequestHeader("Content-Type","text/html;charset=UTF-8");
			
			httpClient.executeMethod(method);
			method.getParams().setContentCharset("UTF-8");

//			String responseBody = method.getResponseBodyAsString();
			
			InputStream resStream = method.getResponseBodyAsStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(resStream));
			StringBuffer resBuffer = new StringBuffer();
			String resTemp = "";
			while ((resTemp = br.readLine()) != null) {
				resBuffer.append(resTemp);
			}
			String responseBody = resBuffer.toString();
			
			br.close();
			resStream.close();
			
			if(responseBody.contains("verify"))
				LOG.info(responseBody);
			
			return responseBody;
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if( method != null)
				method.releaseConnection();
		}
		return null;
	}

}
